package me.helllp.dake.core.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class DefaultCacheManager implements IDakeCacheManager{
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public void putCache(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    @Override
    public void putCache(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public boolean cleanCache(String key) {
        return redisTemplate.delete(key);
    }

    @Override
    public Object getCache(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean putSet(String key, Set<String> sets) {
        redisTemplate.delete(key);
        redisTemplate.opsForSet().add(key, sets);
        return false;
    }
}
